package com.atguigu.springboot.controller;

import com.atguigu.springboot.bean.Employee;
import com.atguigu.springboot.service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Smexy on 2022/10/11
 */
@RestController
public class EmployeeController
{

    @Autowired
    private EmployeeServiceImpl service;

    // op=insert&id=2&lastname=ponyma&gender=male&email=Tom1%40163.com
    @PostMapping(value = "/employee")
    public Object handle1(String op,Integer id,String lastname,String gender,String email){

        Employee e = new Employee(id, lastname, gender, email);

        switch (op){
            case "select" : if (id == null){
                return "id非法!";
            }else {
                Employee employee = service.getById(id);

                return employee == null ? "查无此人" : employee;
            }

            case "insert": service.save(e);
                    return "成功";
            case "update":
                if (id == null){
                    return "id非法!";
                }else {
                    service.saveOrUpdate(e);

                    return "成功";
                }

            case "delete" : if (id == null){
                return "id非法!";
            }else {
                service.removeById(id);

                return "成功";
            }

                default: return "成功";
        }

    }

    @GetMapping(value = "/getAllEmp")
    public Object handle2(){

        return service.list();

    }


}
